package com.ccys.cms.service.masterData.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ccys.cms.service.masterData.SpecialisationService;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.masterData.SpecialisationDelDto;
import com.ccys.db.dto.masterData.SpecialisationPageQueryDto;
import com.ccys.db.dto.masterData.SpecialisationSaveDto;
import com.ccys.db.entity.TblSpecialisation;
import com.ccys.db.mapper.TblSpecialisationMapper;
import com.ccys.db.page.PageDto;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.service.BaseTblAuditTrailWebService;
import com.ccys.db.vo.masterData.SpecialisationListVo;
import com.ccys.db.vo.masterData.SpecialisationPageQueryVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @program: WhiteCoat
 * @description: 专科管理实现类
 * @author: ZhangJingCan
 * @create: 2023-12-13 10:23
 **/

@Service
public class SpecialisationServiceImpl extends BaseServiceImpl<TblSpecialisation> implements SpecialisationService {

    @Resource
    private TblSpecialisationMapper tblSpecialisationMapper;

    /**
     * 用于保存修改状态日志
     */
    @Autowired(required = false)
    private BaseTblAuditTrailWebService baseTblAuditTrailWebService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult saveSpecialisation(SpecialisationSaveDto dto) {
        //需要校验当前集团ID下这个名字是否存在
        if (ObjectUtil.isNotEmpty(dto.getGroupId()) && ObjectUtil.isNotEmpty(dto.getName())) {
            //准备查询条件
            Example example = new Example(TblSpecialisation.class);
            Example.Criteria criteria = example.createCriteria()
                    .andEqualTo("groupId", dto.getGroupId())
                    .andEqualTo("name", dto.getName())
                    .andEqualTo("delFlag", 0);
            //校验是否存在ID - 存在则为修改 - 排除自身
            if (ObjectUtil.isNotEmpty(dto.getId())) {
                criteria.andNotEqualTo("id", dto.getId());
            }
            //查询并判断
            if (ObjectUtil.isNotEmpty(tblSpecialisationMapper.selectOneByExample(example))) {
                return ResponseResult.error("This name exists under the current group!");
            }
        }
        if (ObjectUtil.isNotEmpty(dto.getId()) && ObjectUtil.isNotEmpty(dto.getStatus())) {
            baseTblAuditTrailWebService.saveStatusLog(dto, dto.getStatus());
        }

        //新增/修改 - 并直接返回结果
        return ResponseResult.update(this.insertOrUpdate(dto));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deleteSpecialisation(SpecialisationDelDto dto) {
        //校验参数
        if (ObjectUtil.isEmpty(dto.getId())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备参数进行软删除
        TblSpecialisation specialisation = new TblSpecialisation();
        specialisation.setId(dto.getId());
        specialisation.setDelFlag(1);
        //进行操作并返回是否成功
        if (tblSpecialisationMapper.updateByPrimaryKeySelective(specialisation) > 0) {
            return ResponseResult.success("Delete Successful!");
        }
        return ResponseResult.error("Delete Failed!");
    }

    @Override
    public ResponseResult<PageInfo<SpecialisationPageQueryVo>, Object> findPageSpecialisation(SpecialisationPageQueryDto dto) {
        //判断如果值不在范围则赋值为null
        dto.setStatus(Optional.ofNullable(dto.getStatus()).filter(s -> s > -1 && s < 2).orElse(null));
        PageDto.startPage();
        List<SpecialisationPageQueryVo> list = tblSpecialisationMapper.findPageFlagReason(dto);
        PageInfo<SpecialisationPageQueryVo> info = new PageInfo<>(list);
        return ResponseResult.success(info);
    }

    @Override
    public ResponseResult<List<SpecialisationListVo>, Object> findListSpecialisation(TblSpecialisation dto) {
        List<SpecialisationListVo> list = tblSpecialisationMapper.findListSpecialisation(dto);
        return ResponseResult.success(list);
    }
}
